package com.example.intenttrial;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;

public class MyYouTubeActivity extends FragmentActivity implements
		YouTubePlayer.OnInitializedListener, YouTubePlayer.OnFullscreenListener {

	private static final int RECOVERY_DIALOG_REQUEST = 1;
	public static final String DEVELOPER_KEY = "AIzaSyCBcxi4y0uRnYuoWg1geI_qu_OmukwTN3U";
	private YouTubePlayer player;

	private Button btnPlay;
	private Button btnDuration;
	private boolean fullScreen;
	Provider provider;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.screen_youtube);

		mapUI();
		setListeners();

		YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager()
				.findFragmentById(R.id.youtube_fragment);
		youTubePlayerFragment.initialize(DEVELOPER_KEY, this);
		
	}

	@Override
	  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == RECOVERY_DIALOG_REQUEST) {
	      // Retry initialization if user performed a recovery action
	      provider.initialize(DEVELOPER_KEY, this);
	    }
	  }
	
	@Override
	protected void onResume() {
		super.onResume();

	}

	private void mapUI() {
		btnPlay = (Button) findViewById(R.id.play);
		btnDuration = (Button) findViewById(R.id.duration);
	}

	private void setListeners() {
		btnPlay.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (player.isPlaying()) {
					player.pause();
				} else {
					player.play();
				}
			}
		});

		btnDuration.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				player.setFullscreen(!fullScreen);
			}
		});

	}

	@Override
	public void onFullscreen(boolean isFullscreen) {
		fullScreen = isFullscreen;
	}

	@Override
	public void onInitializationFailure(Provider provider,
			YouTubeInitializationResult errorReason) {
		this.provider = provider;
		if (errorReason.isUserRecoverableError()) {
		      errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
		    } else {
//		      String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
//		      Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
		    }
	}

	@Override
	public void onInitializationSuccess(Provider provider,
			YouTubePlayer player, boolean wasRestored) {
		System.out.println("initialized");
		this.player = player;
		
		this.provider = provider;
		
		player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
		
		player.setOnFullscreenListener(this);
		
		if (!wasRestored) {
			player.loadVideo("RtdFAhJZL5o////");
			player.play();
		}

		player.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {

			@Override
			public void onStopped() {
				btnPlay.setText("play");
			}

			@Override
			public void onSeekTo(int arg0) {

			}

			@Override
			public void onPlaying() {
				btnPlay.setText("stop");
			}

			@Override
			public void onPaused() {
				btnPlay.setText("play");
			}

			@Override
			public void onBuffering(boolean arg0) {

			}
		});

	}

}
